package com.wise.site.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import org.apache.http.conn.util.InetAddressUtils;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.text.format.DateFormat;
import android.widget.Toast;

import com.wise.site.R;
import com.wise.site.WiseSiteApplication;
import com.wise.site.protocol.action.CommentUploadAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;

/**
 * 评论/统计提交工具类
 * 
 * @author Administrator
 * 
 */
public class CommentUtils {
	private static CommentUtils mCommentUtils;
	private CommentUploadAction mUploadAction;
	private ProgressDialog mProgressDialog;
	private SoapAction<Integer> mCountAction = new SoapAction<Integer>(
			ACTION_TYPE.ACTION_COMMON, "flowcount") {
		public Integer parseJson(String response) throws Exception {
			JSONObject json = new JSONObject(response);
			return json.getInt("flag");
		}

	};
	private ActionListener<Integer> commentListener = new ActionListener<Integer>(){
		@Override
		public void onSucceed(Integer result) {
			if(result == 1){
				Toast.makeText(WiseSiteApplication.getContext(), "评论成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(WiseSiteApplication.getContext(), "评论失败", Toast.LENGTH_SHORT).show();
			}
			if(mProgressDialog != null && mProgressDialog.isShowing()){
				mProgressDialog.dismiss();
			}
		}

		@Override
		public void onError(int resultCode) {
			Toast.makeText(WiseSiteApplication.getContext(), "评论失败", Toast.LENGTH_SHORT).show();
			if(mProgressDialog != null && mProgressDialog.isShowing()){
				mProgressDialog.dismiss();
			}
		}
		
	};
	
	protected void showProgress(Context ctx) {
		try{
			if(mProgressDialog == null){
				mProgressDialog = ProgressDialog.show(ctx, null, "评论上传中");
			}else if(!mProgressDialog.isShowing()){
				mProgressDialog.show();
			}
			new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					try{
						mProgressDialog.dismiss();
					}catch (Exception e) {
					}
				}
			}, 16000);
		}catch (Exception e) {
			mProgressDialog = null;
			showProgress(ctx);
		}
	}

	private CommentUtils() {
		mUploadAction = new CommentUploadAction(ACTION_TYPE.ACTION_COMMON,
				"submitcomments");
		mUploadAction.setActionListener(commentListener);
	}

	public static CommentUtils getInstance() {
		if (mCommentUtils == null) {
			mCommentUtils = new CommentUtils();
		}

		return mCommentUtils;
	}

	/**
	 * type 类别(1为供应 2为求购 3为咨讯)
	 * 
	 * @param data
	 */
	public void uploadComment(CommentUploadData data, int type,
			ActionListener listener, Context ctx) {
		mUploadAction.addJsonParam("news_id", data.newsId);
		mUploadAction.addJsonParam("member_id", data.memberId);
		mUploadAction.addJsonParam("cache_username", data.username);
		mUploadAction.addJsonParam("message", data.message);
		mUploadAction.addJsonParam("created",
				DateFormat.format("yyyy-MM-dd hh:mm:ss", new Date()));
		mUploadAction.addJsonParam("type", type);
		mUploadAction.addJsonParam("ip_address", "");

		if(listener != null){
			mUploadAction.setActionListener(listener);
		}
		
		showProgress(ctx);
		ProtocolManager.getProtocolManager().submitAction(mUploadAction);
	}

	/**
	 * * TitleId 栏目ID 
	 *  TitleName 栏目名称
	 *  PhoneType 手机端类型( 1 Android 2 IOS) 
	 *  IpAddress ip地址 
	 *  CreateDate 统计时间
	 * 
	 * huangyue 2012-10-3
	 */
	public void uploadCount(int tabId, int titleId, String titleName){
		mCountAction.addJsonParam("ParentId", tabId);
		mCountAction.addJsonParam("TitleId", titleId);
		mCountAction.addJsonParam("TitleName", titleName);
		mCountAction.addJsonParam("PhoneType", 1);
		mCountAction.addJsonParam("IpAddress", "");
		mCountAction.addJsonParam("CreateDate", DateFormat.format("yyyy-MM-dd hh:mm:ss", new Date()));
		
		ProtocolManager.getProtocolManager().submitAction(mCountAction);
	}

//	public String getLocalIpAddress() {
//		try {
//			String ipv4;
//			ArrayList<NetworkInterface> mylist = Collections
//					.list(NetworkInterface.getNetworkInterfaces());
//			for (NetworkInterface ni : mylist) {
//				ArrayList<InetAddress> ialist = Collections.list(ni
//						.getInetAddresses());
//				for (InetAddress address : ialist) {
//					if (!address.isLoopbackAddress()
//							&& InetAddressUtils.isIPv4Address(ipv4 = address
//									.getHostAddress())) {
//						return ipv4;
//					}
//				}
//			}
//		} catch (SocketException ex) {
//		}
//		return null;
//	}

	/**
	 * news_id 被评论的主题id member_id 评论人id cache_username 评论人名称 ip_address IP 地址
	 * message 评论内容 created 创建时间 type 类别(1为供应 2为求购 3为咨讯)
	 * 
	 * @author Administrator
	 * 
	 */
	public static class CommentUploadData {
		public CommentUploadData() {
		}

		public CommentUploadData(int newsId, int memberId, String username,
				String message) {
			this.newsId = newsId;
			this.memberId = memberId;
			this.username = username;
			this.message = message;
		}

		public int newsId;
		public int memberId;
		public String username;
		public String message;
	}
}
